#include "gamekit/sdl/SDLWindow.h"

#ifdef __WIN32__
#include <SDL.h>
#else
#include <SDL/SDL.h>
#endif

using namespace gamekit;
using namespace gamekit::sdl;

SDLWindow::SDLWindow(const std::string& caption, const ushort& width, const ushort& height, const uint& depth) 
	: Window(caption, width, height, depth),
	m_screen(NULL)
{
}

SDLWindow::~SDLWindow()
{
}

SafePointer<SDLSurface> SDLWindow::screen(void) const
{
	return m_screen;
}

void SDLWindow::onInit(void)
{
	if(SDL_Init( SDL_INIT_EVERYTHING ) == -1)
	{
		return;
	}

	int flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL;

	SDL_Surface* screen = SDL_SetVideoMode( width(), height(), depth(), flags);
	if(screen == NULL)
	{
		return;
	}

	m_screen = new SDLSurface(screen);

	SDL_WM_SetCaption( "Hello World", NULL );
}

void SDLWindow::onBeginDraw(void)
{
	SDL_Rect r;
	r.x = 0;
	r.y = 0;
	r.w = width();
	r.h = height();

	SDL_Surface* surface = m_screen->surface();

	SDL_FillRect(surface, &r, SDL_MapRGB(surface->format, 0, 0, 0));
}

void SDLWindow::onEndDraw(void)
{
	SDL_Flip(m_screen->surface());
}
